package com.feng.gameproj;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class CanvasView extends View implements Runnable 
{
	private float angle = 0.0F;
	
	private Paint paint = null;

	public CanvasView(Context context) 
	{
		super(context);
		this.paint = new Paint ();
		new Thread (this).start ();
	}

	@Override
	protected void onDraw(Canvas canvas) 
	{
		super.onDraw(canvas);
		
		canvas.drawColor(Color.GRAY);
		this.paint.setAntiAlias(true);
		// 设置裁剪区域
		canvas.clipRect(10, 10, 280, 260);
		// 锁定画布
		canvas.save();
		// 旋转画布
		canvas.rotate(this.angle ++);
		
		this.paint.setColor(Color.GREEN);
		canvas.drawRect(15, 15, 140, 70, this.paint);
		// 解除画布锁定
		canvas.restore();
		
		this.paint.setColor(Color.RED);
		canvas.drawRect(150, 75, 260, 120, this.paint);
	}

	@Override
	public void run() 
	{
		while (!Thread.currentThread().isInterrupted())
		{
			try 
			{
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
				Thread.currentThread().interrupt();
			}
			postInvalidate ();
		}
	}

}
